import os
from pathlib import Path
from src.pdf_converter import PDFProcessor
from src.review_system import ReviewSystem

def init_directories():
    """初始化必要的目录"""
    root_dir = Path(__file__).parent  # 获取main.py所在目录
    dirs = [
        root_dir / "rag" / "pdf",
        root_dir / "rag" / "markdown",
        root_dir / "rag" / "vector_store"
    ]
    for dir_path in dirs:
        dir_path.mkdir(parents=True, exist_ok=True)
        print(f"创建目录: {dir_path}")

def main():
    # 初始化目录
    init_directories()
    
    while True:
        print("\n=== 文献综述生成系统 ===")
        print("是否需要先将PDF转换为Markdown格式？")
        print("1. 是，先转换PDF文件")
        print("2. 否，直接进行综述生成")
        print("0. 退出程序")
        
        try:
            choice = input("\n请输入选择(0-2): ").strip()
            
            if choice == "0":
                print("感谢使用，再见！")
                break
                
            if choice == "1":
                print("\n开始PDF转换流程...")
                processor = PDFProcessor()
                processor.convert_pdfs()
                print("\n自动进入综述生成环节...")
                review_system = ReviewSystem()
                run_review_system(review_system)
                break
                
            elif choice == "2":
                print("\n开始综述生成流程...")
                review_system = ReviewSystem()
                run_review_system(review_system)
                break
                
            else:
                print("无效的选择，请重新输入！")
                continue
                
        except KeyboardInterrupt:
            print("\n程序被用户中断")
            break
        except Exception as e:
            print(f"\n发生错误: {str(e)}")
            continue

def run_review_system(review_system):
    """运行综述系统"""
    while True:
        print("\n请选择综述类型：")
        print("1. 技术概念调研综述")
        print("2. 研究现状综述")
        print("3. 多个方法对比分析综述")
        print("4. 技术方法研究脉络综述")
        print("0. 退出")
        
        try:
            choice = input("\n请输入选择(0-4): ").strip()
            
            if choice == "0":
                print("感谢使用，再见！")
                break
                
            if choice == "1":
                concept = input("请输入要调研的技术概念: ").strip()
                if not concept:
                    print("输入不能为空！")
                    continue
                result = review_system.generate_concept_review(concept)
                
            elif choice == "2":
                direction = input("请输入要综述的研究方向: ").strip()
                if not direction:
                    print("输入不能为空！")
                    continue
                result = review_system.generate_research_status(direction)
                
            elif choice == "3":
                methods = input("请输入要对比的方法(用逗号分隔): ").strip()
                if not methods:
                    print("输入不能为空！")
                    continue
                methods_list = [m.strip() for m in methods.split(",") if m.strip()]
                if len(methods_list) < 2:
                    print("请至少输入两个方法进行对比！")
                    continue
                result = review_system.generate_comparative_analysis(methods_list)
                
            elif choice == "4":
                tech = input("请输入要分析的技术: ").strip()
                if not tech:
                    print("输入不能为空！")
                    continue
                result = review_system.generate_technical_evolution(tech)
                
            else:
                print("无效的选择，请重新输入！")
                continue
                
            # 直接打印结果
            print("\n" + "="*50)
            print(result)
            print("="*50 + "\n")
            
        except KeyboardInterrupt:
            print("\n程序被用户中断")
            break
        except Exception as e:
            print(f"\n发生错误: {str(e)}")
            continue

if __name__ == "__main__":
    main()
